using Assets.Scripts.System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class C_CoolingSystem : C_Base
{
    public Vector3 reloadGOLocPos;
    public string drawBoltSoundID = "10003";
    public string reloadSoundID = "10004";
    public string endReloadOrDrawBoltSoundID = "10005";
    private ReloadGOController reloadGOController;
    private C_AnimationController animationController;

    string callbackFuncNames = "CoolingAttack,CoolingSkill,CoolingReload";
    public override void Init(params Object[] args)
    {
        reloadGOController = GetComponent<C_BasicAttribute>()?.C_HolderGO.transform.Find("ReloadGO(Clone)")?.GetComponent<ReloadGOController>();
        if (reloadGOController == null)
        {
            reloadGOController = Instantiate(Resources.Load("Prefabs/ReloadGO") as GameObject, transform).GetComponent<ReloadGOController>();
            reloadGOController.Init(reloadGOLocPos, GetComponent<C_BasicAttribute>()?.C_HolderGO.transform);
        }
        base.Init(args);
    }
    public override void OnAddComponent()
    {
        base.OnAddComponent();
        componentManager.AddCallbackFuncName(callbackFuncNames);
    }
    public override void OnRemoveComponent()
    {
        base.OnRemoveComponent();
        componentManager.RemoveCallbackFuncName(callbackFuncNames);
    }
    public override void Equip(params Object[] args)
    {
        base.Equip();
    }
    public override void UnEquip(params Object[] args)
    {
        base.UnEquip();
    }
    private void OnDestroy()
    {
        //Destroy(reloadGOController.gameObject);
    }
    private void Start()
    {
        animationController = GetComponent<C_AnimationController>();
    }
    public void CoolingAttack(object[] args)
    {
        float attackCoolingTime = (float)args[0];
        float drawBoltTime = (float)args[1];
        StartCoroutine(CoolingAttack_Coro(attackCoolingTime));
        if (drawBoltTime > 0)
        {
            CoolingDrawBolt(drawBoltTime);
        }
    }
    public void CoolingSkill(float skillCoolingTime)
    {
        StartCoroutine(CoolingSkill_Coro(skillCoolingTime));
    }
    public void CoolingDrawBolt(float drawBoltTime)
    {
        animationController?.SetAttackValid(false);
        reloadGOController.StartReload(false, drawBoltTime);
        SoundSystem.Instance.PlaySound(drawBoltSoundID);
    }
    public void CoolingReload(object[] args)
    {
        float reloadTime = (float)args[0];
        float reloadCount = (int)args[1];
        reloadGOController.StartReload(true, reloadTime, () =>
        {
            SoundSystem.Instance.PlaySound(endReloadOrDrawBoltSoundID);
            SendMessage("Reload_", reloadCount);
        });
        SoundSystem.Instance.PlaySound(reloadSoundID);
    }
    public IEnumerator CoolingAttack_Coro(float coolingTime)
    {
        animationController?.SetAttackValid(false);
        if (coolingTime > 0)
        {
            yield return new WaitForSeconds(coolingTime);
        }
        animationController?.SetAttackValid(true);
    }
    public IEnumerator CoolingSkill_Coro(float coolingTime)
    {
        animationController.SetSkillValid(false);
        if (coolingTime > 0)
        {
            yield return new WaitForSeconds(coolingTime);
        }
        animationController.SetSkillValid(true);
    }
}
